package com.kerrykidz.scmstock.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.kerrykidz.common.Constants;
import com.kerrykidz.common.PrimaryKeyGenerator;
import com.kerrykidz.common.PrimaryKeyGenerator.BusinessType;
import com.kerrykidz.common.service.ICommonService;
import com.kerrykidz.scminvoicing.dao.model.GblScmPrdEntity;
import com.kerrykidz.scminvoicing.dao.model.ScmStgAdjEntity;
import com.kerrykidz.scminvoicing.dao.model.ScmStgadjPrdEntity;
import com.kerrykidz.scmstock.service.IStockCorrectService;
import com.kerrykidz.system.dao.model.GblSecBranEntity;
import com.kerrykidz.system.util.UserUtil;
import com.kerrykidz.system.vo.UserInfo;
import com.kerrykidz.workflow.service.ILeaveWorkflowService;
import com.kerrykidz.workflow.service.IWorkflowService;
import com.kerrykidz.workflow.vo.WorkflowParamVo;
import com.rdp.framework.exception.RDPException;

/**
 * 
 * @ClassName: StockGoodsInController
 * @description: 库存调整控制器
 * @author: huanjiang
 * @date: 2014年9月9日11:13:11
 * @version: V1.0
 * 
 */
@RequestMapping("StockCorrect")
@Controller
public class StockCorrectController {

	@Autowired
	private IStockCorrectService iStockCorrectService;

	@Autowired
	private IWorkflowService workflowService;

	@Autowired
	ILeaveWorkflowService leaveWorkflowService;

	@Autowired
	private ICommonService commonService;

	/**
	 * 跳转到库存调整
	 * 
	 * @return ModelAndView
	 * @throws RDPException
	 */
	@RequestMapping("toStockCorrect")
	public ModelAndView toStockCorrect() throws RDPException {
		ModelAndView mav = new ModelAndView("stock/stock_correct");
		UserInfo userInfo = UserUtil.getUserFromSession();
		if (userInfo != null) {
			String brandId = userInfo.getBranId();
			List<GblSecBranEntity> branList = null;
			if (brandId.toUpperCase().indexOf("ZB") != -1)// 总部
			{
				branList = commonService.getBranListForStock(null);
				mav.addObject("branList", branList);
				for (GblSecBranEntity gblSecBranEntity : branList) {
					brandId = gblSecBranEntity.getPkBranId();
					if (brandId.toUpperCase().indexOf("WX") != -1) {
						mav.addObject("branId", brandId);
					}
				}

			} else {
				branList = commonService.getBranListForStock(brandId);
				mav.addObject("branId", brandId);
				mav.addObject("branList", branList);
			}
		}
		return mav;
	}

	/**
	 * 跳转到新增库存调整
	 * 
	 * @param session
	 * @return
	 * @throws RDPException
	 */
	@RequestMapping("toStockAddCorrect")
	public ModelAndView toStockAddCorrect() throws RDPException {
		ModelAndView mav = new ModelAndView("stock/stock_addCorrect");

		UserInfo userInfo = UserUtil.getUserFromSession();
		if (userInfo != null) {
			String correctId = PrimaryKeyGenerator.getPrimaryKey(
					BusinessType.TZ, userInfo.getAbbrCode());
			// 最新的盘点号
			String newestTake = iStockCorrectService.getNewestTake();
			String nowTime = new SimpleDateFormat("yyyy-MM-dd")
					.format(new Date());
			mav.addObject("userInfo", userInfo);
			mav.addObject("correctId", correctId);
			mav.addObject("newestTake", newestTake);
			mav.addObject("nowTime", nowTime);
		}
		return mav;
	}

	/**
	 * 根据库存调整单id查询信息并跳转到编辑调整单页面
	 * 
	 * @param scmStgAdj
	 * @return
	 * @throws RDPException
	 */
	@RequestMapping("toStockaddCorrectX")
	public ModelAndView toStockaddCorrectX(String pkStgadjId)
			throws RDPException {
		ModelAndView mav = new ModelAndView("stock/stock_addCorrect_x");
		UserInfo userInfo = UserUtil.getUserFromSession();
		if (null != userInfo) {
			String savedPrd = "";
			List<ScmStgadjPrdEntity> sspBeanList = iStockCorrectService
					.findScmStgadjPrd(pkStgadjId);
			for (ScmStgadjPrdEntity bean : sspBeanList) {
				// 保存已经调整过的商品id
				if (bean.getFkPrdId() != null) {
					savedPrd += bean.getFkPrdId() + "_";
				}
			}
			ScmStgAdjEntity bean = iStockCorrectService.findScmStgAdjById(
					pkStgadjId, userInfo.getUserId());
			if (null != bean) {
				String applyTime = new SimpleDateFormat("yyyy-MM-dd")
						.format(bean.getApplyTime());
				mav.addObject("scmStgAdj", bean);
				mav.addObject("applyTime", applyTime);
				mav.addObject("userId", userInfo.getUserId());
				mav.addObject("savedPrd", savedPrd);
			}
		}
		return mav;
	}

	/**
	 * 根据url跳转到库存调整处理页面 或者流程历史查看页面
	 * 
	 * @param scmStgAdj
	 * @param session
	 * @return
	 * @throws RDPException
	 */
	@RequestMapping("toStockShenheCorrect")
	public ModelAndView toStockShenheCorrect(String pkStgadjId, String url)
			throws RDPException {
		ModelAndView mav = new ModelAndView(url);
		UserInfo userInfo = UserUtil.getUserFromSession();
		if (null != userInfo) {
			String userId = userInfo.getUserId();
			ScmStgAdjEntity bean = iStockCorrectService.findScmStgAdjById(
					pkStgadjId, userId);
			if (null != bean) {
				String applyTime = new SimpleDateFormat("yyyy-MM-dd")
						.format(bean.getApplyTime());
				mav.addObject("scmStgAdj", bean);
				mav.addObject("applyTime", applyTime);
			}
		}
		return mav;
	}

	/**
	 * 库存调整的分页和查询
	 * 
	 * @param startSearchTime
	 * @param endSearchTime
	 * @param scmStgAdj
	 * @return
	 * @throws RDPException
	 */
	@RequestMapping(value = "findScmStgAdj", method = RequestMethod.POST)
	@ResponseBody
	public ScmStgAdjEntity findScmStgAdj(String startSearchTime,
			String endSearchTime, ScmStgAdjEntity scmStgAdj)
			throws RDPException {
		UserInfo userInfo = UserUtil.getUserFromSession();
		if (userInfo == null) {
			return null;
		}
		scmStgAdj.setUserId(userInfo.getUserId());
		return iStockCorrectService.findScmStgAdj(startSearchTime,
				endSearchTime, scmStgAdj, userInfo.getUserId());
	}

	/**
	 * 库存调整清单保存
	 * 
	 * @param: contents
	 * @param scmStgadjPrd
	 * @throws RDPException
	 */
	@RequestMapping(value = "saveScmStgadjPrds", method = RequestMethod.POST)
	@ResponseBody
	public String saveScmStgadjPrds(String contents, ScmStgAdjEntity scmStgAdj)
			throws RDPException {
		return iStockCorrectService.saveScmStgadjPrds(contents, scmStgAdj);
	}

	/**
	 * 库存调整清单提交
	 * 
	 * @param contents
	 * @param scmStgAdj
	 * @return
	 * @throws RDPException
	 */
	@RequestMapping(value = "submitScmStgadjPrds", method = RequestMethod.POST)
	@ResponseBody
	public String submitScmStgadjPrds(String contents, ScmStgAdjEntity scmStgAdj)
			throws RDPException {
		UserInfo user = UserUtil.getUserFromSession();
		if (user == null) {
			return null;
		}
		return iStockCorrectService.submitScmStgadjPrds(contents, scmStgAdj,
				user);
	}

	/**
	 * 库存调整草稿删除
	 * 
	 * @param scmStgAdj
	 * @param session
	 * @return
	 * @throws RDPException
	 */
	@RequestMapping(value = "delScmStgadjPrds", method = RequestMethod.POST)
	@ResponseBody
	public String delScmStgadjPrds(ScmStgAdjEntity scmStgAdj)
			throws RDPException {
		UserInfo userInfo = UserUtil.getUserFromSession();
		if (userInfo == null) {
			return null;
		}
		scmStgAdj.setStatus("0");
		scmStgAdj.setModiBy(userInfo.getUserId());
		return iStockCorrectService.updateScmStgAdj(scmStgAdj);
	}

	/**
	 * 获得已经调整的商品
	 * 
	 * @param fkStgadjId
	 * @param fkBranId
	 * @return
	 * @throws RDPException
	 */
	@RequestMapping(value = "getSavedCorrectPrds", method = RequestMethod.POST)
	@ResponseBody
	public List<GblScmPrdEntity> getSavedCorrectPrds(String fkStgadjId,
			String fkBranId) throws RDPException {
		return iStockCorrectService.getSavedCorrectPrds(fkStgadjId, fkBranId);
	}

	/**
	 * 库存调整-草稿-编辑
	 * 
	 * @param contents
	 *            添加或需要更新的调整商品信息
	 * @param scmStgAdj
	 *            操作的库存调整单
	 * @param isSubmit
	 *            是否是提交 true 提交 false 保存
	 * @param session
	 * @param savedPrd
	 *            判断商品时候已经保存过
	 * @return
	 * @throws RDPException
	 */
	@RequestMapping(value = "saveScmStgadjPrdsForEdit", method = RequestMethod.POST)
	@ResponseBody
	public String saveScmStgadjPrdsForEdit(WorkflowParamVo workflowparam,
			String contents, ScmStgAdjEntity scmStgAdj, String isSubmit,
			String savedPrd) throws RDPException {
		UserInfo user = UserUtil.getUserFromSession();
		if (user == null) {
			return null;
		}
		HashMap<String, String> prdMap = new HashMap<String, String>();
		String[] prdStr = savedPrd.split("_");
		for (String s : prdStr) {
			prdMap.put(s, s);
		}
		workflowparam.setUserInfo(user);
		return iStockCorrectService.saveScmStgadjPrdsForEdit(workflowparam,
				contents, scmStgAdj, isSubmit, user, prdMap);
	}

	/**
	 * 递交
	 * 
	 * @param taskId
	 * @param var
	 * @return
	 */
	@RequestMapping(value = "completeStockCorrect", method = RequestMethod.POST)
	public ModelAndView completeStockCorrect(WorkflowParamVo workflowparam,
			String fkStgadjId, String fkBranId, String modiBy)
			throws RDPException {
		UserInfo userInfo = UserUtil.getUserFromSession();
		if (null != workflowparam) {
			if (null != userInfo
					&& StringUtils.isNotBlank(workflowparam.getBusinessKey())) {
				workflowparam.setUserInfo(userInfo);
				iStockCorrectService.completeStockCorrect(workflowparam,
						fkBranId, modiBy);
				return toStockCorrect();
			}
		}
		return null;
	}

	/**
	 * 根据调整单号和商品号删除调整清单项
	 * 
	 * @param scmStgadjPrd
	 * @throws RDPException
	 */
	@RequestMapping(value = "delStgadjPrds", method = RequestMethod.POST)
	@ResponseBody
	public String delStgadjPrds(ScmStgadjPrdEntity scmStgadjPrd)
			throws RDPException {
		iStockCorrectService.delScmStgadjPrd(scmStgadjPrd);
		return Constants.SUCCESS;
	}
}
